package com.xcity.game.recharge;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xcity.db.entity.Player;
import com.xcity.game.activity.mooncard.MoonCardActivityService;
import com.xcity.game.common.PlayerPool;
import com.xcity.pomelo.PomeloRoute;
import com.xcity.pomelo.request.GetRechargeListRequest;
import com.xcity.pomelo.response.RechargeListResponse;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.net.Session;
import naga.x.net.annotation.OPHandler;
import naga.x.net.impl.pomelo.annotation.PomeloOP;
import naga.x.service.Service;
import naga.x.service.ServiceException;

@OPHandler
public class RechargeService implements Service {

	protected static final Logger LOG = LoggerFactory.getLogger(RechargeService.class);
	
	private List<RechargeTemplate> templates;
	
	@Override
	public void startup() throws ServiceException {
		templates = App.getApp().getServiceManager().get(TemplateService.class).getTemplatesList(RechargeTemplate.class);
	}

	@Override
	public void shutdown() throws ServiceException {
	}

	@Override
	public String getId() {
		return getClass().getName();
	}

	@PomeloOP(PomeloRoute.RECHARGE_LIST)
	protected void requestList(Session session, GetRechargeListRequest req) {
		Player player = session.getClient();
		RechargeListResponse.Filter filter = null;
		if (player.getPool().getInt(PlayerPool.PROPERTY_LAST_RECHARGE_YEAR, 0) > 0) { // 充过终身卡
			filter = new RechargeListResponse.Filter() {
				@Override
				public boolean doFilter(int id) {
					return id != MoonCardActivityService.YEARCARD_TEMP_ID;
				}
			};
		}
		session.send(new RechargeListResponse(req.getSerial(), player, templates, filter));
	}
	
//	@Deprecated
//	@PomeloOP(PomeloRoute.WEIXIN_RECHARGE_OLD)
//	protected void requestRechargeWxOld(Session session, DoRechargeRequest req) {
//		int productId = NumberUtils.parseInt(req.getRechargeId());
//		RechargeTemplate template = App.getApp().getServiceManager().get(TemplateService.class).getTemplates(RechargeTemplate.class).get(productId);
//		if (template == null) {
//			LOG.info("[RECHARGE ERROR]DETAIL[{}]CHANNEL[WEIXIN]CAUSE[TEMPLATE NOT FOUND]", JSON.toJSONString(req));
//			return;
//		}
//		long playerId = NumberUtils.parseLong(req.getUserId());
//		Player player = App.getApp().getServiceManager().get(ActorService.class).find(playerId);
//		int oldVal;
//		if (player == null) {
//			LOG.error("[RECHARGE ERROR]DATA[{}]CAUSE[PLAYER NOT FOUND]", JSON.toJSONString(req));
//			return;
//		}
//		if (player.isOnline()) {
//			oldVal = player.getStone();
//			if (template.getStone() > 0) {
//				player.addStone(template.getStone(), Cause.RECHARGE + ":wx");
//				player.send(new RechargeResultPush(template.getStone()));
//			}
//			App.getApp().getEventManager().addEvent(GameEvents.EVENT_RECHARGE, player, template.getPrice(), template);
//		} else {
//			oldVal = player.getStone();
//			if (template.getStone() > 0) {
//				// 离线玩家发邮件
//				App.getApp().getServiceManager().get(MailService.class).sendSystemMail(
//						playerId,
//						MessageConst.RECHARGE_SUCC,
//						MessageFormat.format(MessageConst.RECHARGE_SUCC_MAIL_CONTENT, new Date(), template.getStone()),
//						new MailAttach(0, template.getStone()));
//				App.getApp().getServiceManager().get(PlayerService.class).addOfflineMsg(playerId, MessageConst.RECHARGE_SUCC_OFFLINE_TIP);
//				// 记录离线充值，用于成就系统
//				JSONArray arr = player.getPool().getJSONArray(PlayerPool.PROPERTY_OFFLINE_RECHARGE);
//				if (arr == null) {
//					arr = new JSONArray(2);
//				}
//				arr.add(new int[]{ template.getPrice(), template.getId() });
//				player.poolSet(PlayerPool.PROPERTY_OFFLINE_RECHARGE, arr);
//			}
//			App.getApp().getEventManager().addEvent(GameEvents.EVENT_RECHARGE_OFFLINE, player, template.getPrice(), template);
//		}
//		session.send(new Response(req.getSerial()));
//		LOG.info("[RECHARGE SUCC]DETAIL[{}]CHANNEL[WEIXIN]IP[{}]", JSON.toJSONString(req), session.getRemoteAddress());
//		session.close(false);
//	}
	
}
